﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace CarBlackBox
{
    public partial class Form_DataConllection : Form
    {
        public Form_DataConllection()
        {
            InitializeComponent();
        }

        private void Form_DataConllection_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.Add("默认端口");
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
        }

        private void btSystemTime_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);            
            object[] ret=com.ProcCommand(CommunicationCommand.采集记录仪的实时时钟);
            tbRecordTime.Text = "";
            if (ret != null && ret.Length == 1)
            {
                DateTime dt = (DateTime)ret[0];
                tbRecordTime.Text = dt.ToString();
            }                        
            this.Cursor = Cursors.Default;
        }

        private void btFactor_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            object[] ret = com.ProcCommand(CommunicationCommand.采集记录仪中的车辆特征系数);
            tbFactor.Text = "";
            if (ret != null && ret.Length == 1)
            {
                 uint Factor = (uint)ret[0];
                 tbFactor.Text = Factor.ToString();
            }            
            this.Cursor = Cursors.Default;
        }

        private void btVehicleInfo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            object[] ret = com.ProcCommand(CommunicationCommand.采集车辆VIN号_车牌号码_车牌分类);
            tbVehicleInfo.Text = "";
            if (ret != null && ret.Length == 1)
            {
                string VehicleInfo = (string)ret[0];
                tbVehicleInfo.Text = VehicleInfo;
            }
            this.Cursor = Cursors.Default;
        }

        private void btDriverInfo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            object[] ret = com.ProcCommand(CommunicationCommand.采集驾驶员代码及对应的机动性车驾驶证号码);
            tbDriverInfo.Text = "";
            if (ret != null && ret.Length == 1)
            {
                string DriverInfo = (string)ret[0];
                tbDriverInfo.Text = DriverInfo;
            }
            this.Cursor = Cursors.Default;
        }

        private void bt360Km_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            object[] ret = com.ProcCommand(CommunicationCommand.采集最近360H内的累计行驶里程数据);
            tb360Km.Text = "";
            if (ret != null && ret.Length==1)
            {
                string Km = (string)ret[0];
                tb360Km.Text = Km;
            }
            this.Cursor = Cursors.Default;
        }

        private void bt2DKm_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            object[] ret = com.ProcCommand(CommunicationCommand.采集最近2个日历天内的累计行驶里程);
            tb2DKm.Text = "";
            if (ret != null && ret.Length == 1)
            {
                string Km = (string)ret[0];
                tb2DKm.Text = Km;
            }
            this.Cursor = Cursors.Default;
        }
    }
}
